/***************************************************************************
 创建者: 华磊
 开始时间: 2020.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2020.5.8 类的具体实现代码编写

 ***************************************************************************/
#include "ecatbusmanager.h"
#include "robotcontrol.h"
#include <QDebug>
#include "rfiddata.h"
#include "maganeticsensordata.h"
#include "encoderreceiver.h"
#include "messagelog.h"
#include "robotbase.h"

QMap<u64,int> myMap;
void setMapVal(int vendorId,int productCode, int val){
    u64 key = ((u64)vendorId)<<32 | (u64)productCode;
    myMap[ key]= 1;
}

int getMapVal(int vendorId,int productCode){
    u64 key = ((u64)vendorId)<<32 | (u64)productCode;
    return myMap.value(key);
}

EcatBusManager::EcatBusManager( RobotControl* robctl )
    :robCtl(robctl)
{
    isBusDevXmlFileReadOk=false;
    isInitialOkStatus=false;
    isCheckAlias=true;


    ethercatDeviceSize=0;
    _isMapOk=false;
    _isLinkOk=false;
    memset(nodeToRobotIdAndAxis, 0, D_MAX_SLAVES * sizeof(SRobotIdAndAxit));

    for(int i=0; i<D_MAX_SLAVES; i++)
    {
        ec_devInfo[i].devInfo.alias=0;
        ec_devInfo[i].devInfo.vendorId=0;
        ec_devInfo[i].devInfo.vendorId=0;
        ec_devInfo[i].devInfo.productCode=0;
        ec_devInfo[i].devInfo.revisionNumber=0;
        ec_devInfo[i].devInfo.state=0;
        ec_devInfo[i].devInfo.devType=0;
    }

    dcCycle=1000;
    dcCycle_modbus_rtu=0;
    dcCycle_modbus_tcp=0;


}

int EcatBusManager::getEthercatSlavesInformation(std::vector<EcatBusDevInfo_gsoap> &slavesInfo)
{
    slavesInfo.resize(ethercatDeviceSize);
    EcatBusDevInfo_gsoap tmpDeviceInfo;
    for(int i=0;i<ethercatDeviceSize;i++)
    {
        tmpDeviceInfo.node=ec_devInfo[i].devInfo.node;
        tmpDeviceInfo.alias=ec_devInfo[i].devInfo.alias;
        tmpDeviceInfo.vendorId=ec_devInfo[i].devInfo.vendorId;
        tmpDeviceInfo.productCode=ec_devInfo[i].devInfo.productCode;
        tmpDeviceInfo.revisionNumber=ec_devInfo[i].devInfo.revisionNumber;
        tmpDeviceInfo.al_state=ec_devInfo[i].devInfo.state;
        tmpDeviceInfo.devType=ec_devInfo[i].devInfo.devType;
        tmpDeviceInfo.devName=ec_devInfo[i].devInfo.devName.toStdString();
        tmpDeviceInfo.mappingObjectFile=ec_devInfo[i].devInfo.mappingObjectFile.toStdString();
        slavesInfo.push_back(tmpDeviceInfo);
    }
    return 1;
}

int EcatBusManager::initial(int ethercatLibType,  bool isUseEthercatDeivce,
                                bool isUseModbusRtuDeivce, bool isUseModbusTcpDeivce, bool isUseCanopenDeivce)
{
    QString rootFilePath=D_TCRCONTROLLER_DATA_PATH;
    QString filePath;

#ifdef D_USE_IGH
    if(1==ethercatLibType && true==isUseEthercatDeivce)
    {
        filePath=rootFilePath+GLOBAL_ECAT_BUS_PATH;
        if(1!=readEcatBusXml(filePath+"busDevs.xml"))
        {
            isBusDevXmlFileReadOk=false;
            return -1;
        }
    }
#endif

#ifdef D_USE_ACONTIS
    if(2==ethercatLibType && true==isUseEthercatDeivce)
    {
        filePath=rootFilePath+GLOBAL_ECAT_BUS_PATH;
        readCmdXml(filePath+"cmdline.xml");
        if(1!=readEcatBusXml(filePath+"busDevs.xml"))
        {
            return -1;
        }
    }
#endif

#ifdef D_USE_ONLY_VREP
    if(3==ethercatLibType && true==isUseEthercatDeivce)
    {
        filePath=rootFilePath+GLOBAL_VIRTUAL_DEVICE_PATH;
        if(1!=readVirtualDeviceXml(filePath+"busDevs.xml"))
        {
            isBusDevXmlFileReadOk=false;
            return -1;
        }
    }
#endif



    #ifdef D_USE_MODBUS_RTU
    if(true==isUseModbusRtuDeivce)
    {
        filePath=rootFilePath+GLOBAL_MODBUS_RTU_PATH;
        if(1!=readModBusRtuXml(filePath+"busDevs.xml"))
        {
            isBusDevXmlFileReadOk=false;
           return -2;
        }
    }
    #endif

    #ifdef D_USE_MODBUS_TCP
    if(true==isUseModbusTcpDeivce)
    {
        filePath=rootFilePath+GLOBAL_MODBUS_TCP_PATH;
        if(1!=readModBusTcpXml(filePath+"busDevs.xml"))
        {
            isBusDevXmlFileReadOk=false;
           return -3;
        }
    }
    #endif

    #ifdef D_USE_CANOPEN
    if(true==isUseCanopenDeivce)
    {
        filePath=rootFilePath+GLOBAL_CANOPEN_PATH;
        if(1!=readCanopenBusXml(filePath+"busDevs.xml"))
        {
            isBusDevXmlFileReadOk=false;
           return -4;
        }
    }
    #endif


    //ethercat motor 和　io对象
    ec_motorSize=0;
    ec_ioModuleSize=0;
    encoderReceiverSize=0;

    for(int i=0;i<deviceTypeList_ethercat.size();i++)
    {
        if(E_EcatDev_Servo==deviceTypeList_ethercat[i])
        {
            ec_motorSize++;
            EcatMotor* tmpMotor=new EcatMotor(E_DEVICE_PROTOCOL_ETHERCAT,aliasList_ethercat[i],0,deviceChannel_ethercat[i]);
            QVector<SdoConfig> sdoConfigList;
            if(1==readMotorConfigXml(xmlFilelist_ethercat[i],sdoConfigList))
            {
                tmpMotor->setSdoList(sdoConfigList);
            }
            else
            {
                addMsg(ENUM_MSG_ERROR,"EcatBusManager","EcatBusManager",12353,-1,0,0,0,0,xmlFilelist_ethercat[i]);
            }

            ec_motors.append(tmpMotor);
        }
        else if(E_EcatDev_IoModule==deviceTypeList_ethercat[i])
        {
            ec_ioModuleSize++;
            EcatIo* tmpIomodule=new EcatIo(E_DEVICE_PROTOCOL_ETHERCAT,aliasList_ethercat[i],0);

            QVector<SdoConfig> sdoConfigList;
            if(1==readMotorConfigXml(xmlFilelist_ethercat[i],sdoConfigList))
            {
                tmpIomodule->setSdoList(sdoConfigList);
            }
            else
            {
                addMsg(ENUM_MSG_ERROR,"EcatBusManager","EcatBusManager",12354,-1,0,0,0,0,xmlFilelist_ethercat[i]);
            }


            ec_ioModule.append(tmpIomodule);
            ec_ioModuleArry[ec_ioModuleSize-1]=tmpIomodule;
        }
        else if(E_EcatDev_EncoderReceiver==deviceTypeList_ethercat[i])
        {
            encoderReceiverSize++;
            //内部读取sdo配置
            EncoderReceiver* tmpReceiver=new EncoderReceiver(E_DEVICE_PROTOCOL_ETHERCAT,aliasList_ethercat[i],xmlFilelist_ethercat[i]);
            if(true!=tmpReceiver->isInitialOkStatus())
            {
                encoderReceiverSize--;
                addMsg(ENUM_MSG_ERROR,"EcatBusManager","EcatBusManager",12351,-1);
                return -10;
            }
            encoderReceivers.append(tmpReceiver);
            encoderReceiverArry[encoderReceiverSize-1]=tmpReceiver;
        }
    }


    //modbus_rtu motor 和　io对象
    modbus_rtu_motorSize=0;
    modbus_rtu_ioModuleSize=0;

    for(int i=0;i<deviceTypeList_modbus_rtu.size();i++)
    {
        if(E_EcatDev_Servo==deviceTypeList_modbus_rtu[i])
        {
            modbus_rtu_motorSize++;
            EcatMotor* tmpMotor=new EcatMotor(E_DEVICE_PROTOCOL_MODBUS_RTU,aliasList_modbus_rtu[i],0,1);
            modbus_rtu_motors.append(tmpMotor);
        }
        else if(E_EcatDev_IoModule==deviceTypeList_modbus_rtu[i])
        {
            modbus_rtu_ioModuleSize++;
            EcatIo* tmpIomodule=new EcatIo(E_DEVICE_PROTOCOL_MODBUS_RTU,aliasList_modbus_rtu[i],0);
            modbus_rtu_ioModule.append(tmpIomodule);
        }
    }


    //modbus_tcp motor 和　io对象
    modbus_tcp_motorSize=0;
    modbus_tcp_ioModuleSize=0;

    for(int i=0;i<deviceTypeList_modbus_tcp.size();i++)
    {
        if(E_EcatDev_Servo==deviceTypeList_modbus_tcp[i])
        {
            modbus_tcp_motorSize++;
            EcatMotor* tmpMotor=new EcatMotor(E_DEVICE_PROTOCOL_MODBUS_TCP,aliasList_modbus_tcp[i],0,1);
            modbus_tcp_motors.append(tmpMotor);
        }
        else if(E_EcatDev_IoModule==deviceTypeList_modbus_tcp[i])
        {
            modbus_tcp_ioModuleSize++;
            EcatIo* tmpIomodule=new EcatIo(E_DEVICE_PROTOCOL_MODBUS_TCP,aliasList_modbus_tcp[i],0);
            modbus_tcp_ioModule.append(tmpIomodule);
        }
    }


    //canopen motor 和　io rfid maganetic 对象
    canopen_motorSize=0;//canopen
    canopen_ioModuleSize=0;//canopen
    canopen_rfidSize=0;//canopen
    canopen_maganeticSize=0;//canopen
    canopen_batterySize=0;

    for(int i=0;i<deviceTypeList_canopen.size();i++)
    {
        if(E_EcatDev_Servo==deviceTypeList_canopen[i])
        {
            canopen_motorSize++;
            EcatMotor* tmpMotor=new EcatMotor(E_DEVICE_PROTOCOL_CANOPEN,aliasList_canopen[i],0,deviceChannel_canopen[i]);
            qDebug()<<"vendorId_canopen[i]"<<vendorId_canopen[i];
            tmpMotor->setVendorId(vendorId_canopen[i]);
            canopen_motors.append(tmpMotor);
        }
        else if(E_EcatDev_IoModule==deviceTypeList_canopen[i])
        {
            canopen_ioModuleSize++;
            EcatIo* tmpIomodule=new EcatIo(E_DEVICE_PROTOCOL_CANOPEN,aliasList_canopen[i],0);
            //setVendorId?
            canopen_ioModule.append(tmpIomodule);
        }
        else if(E_EcatDev_rfidSensor==deviceTypeList_canopen[i])
        {
            canopen_rfidSize++;
            RfidData* tmpRfidData=new RfidData(E_DEVICE_PROTOCOL_CANOPEN,aliasList_canopen[i],
                                               busIdList_canopen[i],deviceIdList_canopen[i]);
            tmpRfidData->setVendorId(vendorId_canopen[i]);
            canopen_rfidData.append(tmpRfidData);
        }
        else if(E_EcatDev_maganeticSensor==deviceTypeList_canopen[i])
        {
            canopen_maganeticSize++;
            MaganeticSensorData* tmpMaganetic=new MaganeticSensorData(E_DEVICE_PROTOCOL_CANOPEN,aliasList_canopen[i],
                                                         busIdList_canopen[i],deviceIdList_canopen[i]);
            tmpMaganetic->setVendorId(vendorId_canopen[i]);
            canopen_maganeticData.append(tmpMaganetic);
        }
        else if(E_EcatDev_batterySensor==deviceTypeList_canopen[i])
        {
            canopen_batterySize++;

        }
    }


    //dcCycle, data_wave也用
    if(0==ec_motors.size() && 0!=modbus_rtu_motors.size())
    {
        dcCycle=dcCycle_modbus_rtu;
    }
    else if(0==ec_motors.size() && 0==modbus_rtu_motors.size()
            && 0!=modbus_tcp_motors.size())
    {
        dcCycle=dcCycle_modbus_tcp;
    }
    else if(0==ec_motors.size() && 0==modbus_rtu_motors.size()
            && 0==modbus_tcp_motors.size() && 0!=canopen_motors.size())
    {
        dcCycle=dcCycle_canopen;
    }
    else if(0==ec_motors.size() && 0==modbus_rtu_motors.size()
            && 0==modbus_tcp_motors.size() && 0==canopen_motors.size())
    {
         dcCycle=10000;
    }



    isInitialOkStatus=true;
    isBusDevXmlFileReadOk=true;
    return 1;
}

//int EcatBusManager::getModbusTcpIoPointer(int alias,EcatIo* ioModulePointer)
//{

//}

//int EcatBusManager::getModbusRtuIoPointer(int alias,EcatIo* ioModulePointer)
//{

//}

//int EcatBusManager::getModbusTcpMotorPointer(int alias,EcatMotor* motorPointer)
//{

//}

//int EcatBusManager::getModbusRtuMotorPointer(int alias,EcatMotor* motorPointer)
//{

//}


#ifdef D_USE_ACONTIS
int EcatBusManager::readCmdXml( QString xmlfile )
{
    bool ok=openXml( xmlfile, QFile::ReadOnly | QFile::Text );
    if(!ok)
    {
        qDebug()<<"ERROR: openXml :"<<ok<< xmlfile;
        return -1;
    }

    QDomNodeList nodelist = m_root.childNodes();
    int count = nodelist.size();
    if(count < 1)
    {
        qDebug()<<"ERROR: bus count<1 !";
        closeXml();
        return -2;
    }
    ok=false;
    for(int i = 0; i < count; i++ )
    {
        QDomNode node = nodelist.at(i);
        if( node.nodeName() .indexOf("#comment")>=0){
            continue;
        } else if( node.nodeName().contains("cmd")){
            if( !node.firstChild().isNull() )
            {
                cmdLine=node.firstChild().nodeValue();
                ok=true;
            }
        }else if( node.nodeName().contains("eni")){
            if( !node.firstChild().isNull() )
            {
                eniFile=node.firstChild().nodeValue();
                ok=true;
            }
        }
    }

    qDebug()<<"read cmdline:"<< cmdLine <<eniFile;
    closeXml();
    return ok?1:-1;
}
#endif

bool EcatBusManager::isInitialOk()
{
    return isInitialOkStatus;
}


bool EcatBusManager::getXmlFilesStatus()
{
    return isBusDevXmlFileReadOk;
}


int EcatBusManager::readEcatBusXml( QString xmlfile )
{
    bool ok=openXml( xmlfile, QFile::ReadOnly | QFile::Text );
    if(!ok)
    {
        qDebug()<<"readBusXml error "<<ok;
        return -1;
    }
    aliasList_ethercat.clear();
    deviceTypeList_ethercat.clear();
    xmlFilelist_ethercat.clear();
    deviceChannel_ethercat.clear();
    int ethercatIoDeviceVectorIndex=-1;
    int ethercatEncoderDeviceVectorIndex=-1;
    int ethercatMotorDeviceVectorIndex=-1;


    QDomNode cycleNode=findSubNode( m_root, "dcCycle", ok );
    if(!ok)
    {
       qDebug()<<"不可用的总线配置,busDevs.xml文件格式dcCycle有误!";
       return -2;
    }
    readXml( cycleNode, dcCycle );

    QDomNodeList devNodes = m_root.childNodes();
    int alias = 0;
    int  devtype ;
    QString tmpStr;
    int tmpChannel;
    QString filepath =D_TCRCONTROLLER_DATA_PATH;
    filepath+=GLOBAL_ECAT_BUS_PATH;
    QString fileName ;
    for(int i=0;i<devNodes.size()-1;i++)
    {
        QDomNode devND =  devNodes.at(i);
        if( devND.nodeName() .indexOf("#comment")>=0){
            continue;
        }


        readXml( findSubNode( devND, "alias", ok ), alias );
        if(!ok)
        {
           return -100-i;
        }
        readXml( findSubNode( devND, "xmlFile", ok ), fileName );
        if(!ok)
        {
           return -100-i;
        }
        readXml( findSubNode( devND, "deviceType", ok ), tmpStr );
        if(!ok)
        {
           return -100-i;
        }
        readXml( findSubNode( devND, "deviceChannel", ok ), tmpChannel );
        if(!ok)
        {
           qDebug()<<"error,no find node deviceChannel!!!";
           return -100-i;
        }



        if( tmpStr.indexOf("servo",Qt::CaseInsensitive)>=0 )
        {
            devtype=E_EcatDev_Servo;
            ethercatMotorDeviceVectorIndex+=1;
            ethercat_nodeAndVectorIndex.insert(i,ethercatMotorDeviceVectorIndex);

        }
        else  if( tmpStr.indexOf("coupler",Qt::CaseInsensitive)>=0 )
        {
            devtype=E_EcatDev_Coupler;
        }
        else  if( tmpStr.indexOf("iomodule",Qt::CaseInsensitive)>=0 )
        {
            devtype=E_EcatDev_IoModule;
            ethercatIoDeviceVectorIndex+=1;
            ethercat_nodeAndVectorIndex.insert(i,ethercatIoDeviceVectorIndex);
        }
        else  if( tmpStr.indexOf("encoderReceiver",Qt::CaseInsensitive)>=0 )
        {
            devtype=E_EcatDev_EncoderReceiver;
            ethercatEncoderDeviceVectorIndex+=1;
            ethercat_nodeAndVectorIndex.insert(i,ethercatEncoderDeviceVectorIndex);
        }
        else
        {
            addMsg(ENUM_MSG_ERROR,"EcatBusManager","EcatBusManager",12352,-1,0,0,0,0,tmpStr);
            devtype=E_EcatDev_New;
        }

        if( ok )
        {
            xmlFilelist_ethercat.push_back(filepath+fileName);
            deviceTypeList_ethercat.push_back( devtype);
            aliasList_ethercat.push_back(alias);
            deviceChannel_ethercat.push_back(tmpChannel);
        }
        else
        {
            qDebug()<<"不可用的总线配置,busDevs.xml文件格式有误!"<<i<<alias;
            return -3;
        }
    }

    closeXml();

    return 1;
}


int EcatBusManager::readCanopenBusXml( QString xmlfile )
{
    bool ok=openXml( xmlfile, QFile::ReadOnly | QFile::Text );
    if(!ok)
    {
        qDebug()<<"readBusXml error "<<ok;
        return -1;
    }
    aliasList_canopen.clear();
    deviceTypeList_canopen.clear();
    xmlFilelist_canopen.clear();
    deviceChannel_canopen.clear();
//    int ethercatIoDeviceVectorIndex=-1;
//    int ethercatMotorDeviceVectorIndex=-1;


    QDomNode cycleNode=findSubNode( m_root, "dcCycle", ok );
    if(!ok)
    {
       qDebug()<<"不可用的总线配置,busDevs.xml文件格式dcCycle有误!";
       return -2;
    }
    readXml( cycleNode, dcCycle_canopen );

    QDomNodeList devNodes = m_root.childNodes();
    int alias = 0;
    int  devtype ;
    int deviceId;
    int busId;
    int vendorId;
    int tmpChannel;
    QString tmpStr;
    QString filepath =D_TCRCONTROLLER_DATA_PATH;
    filepath+=GLOBAL_CANOPEN_PATH;
    QString fileName ;
    for(int i=0;i<devNodes.size()-1;i++)
    {
        QDomNode devND =  devNodes.at(i);
        if( devND.nodeName() .indexOf("#comment")>=0){
            continue;
        }


        readXml( findSubNode( devND, "alias", ok ), alias );
        if(!ok)
        {
           return -100-i;
        }
        QDomNode tmpNode=findSubNode( devND, "alias", ok );
        if(false==tmpNode.toElement().hasAttribute("deviceId" ))
        {
            return -10;
        }
        deviceId = tmpNode.toElement().attribute("deviceId" ).toUInt();
        if(false==tmpNode.toElement().hasAttribute("busId" ))
        {
            return -10;
        }
        busId = tmpNode.toElement().attribute("busId" ).toUInt();
        if(false==tmpNode.toElement().hasAttribute("vendorId" ))
        {
            return -10;
        }
        vendorId = tmpNode.toElement().attribute("vendorId" ).toUInt();

        readXml( findSubNode( devND, "xmlFile", ok ), fileName );
        if(!ok)
        {
           return -100-i;
        }
        readXml( findSubNode( devND, "deviceType", ok ), tmpStr );
        if(!ok)
        {
           return -100-i;
        }

        readXml( findSubNode( devND, "deviceChannel", ok ), tmpChannel );
        if(!ok)
        {
            qDebug()<<"error,no find node deviceChannel!!!";
           return -100-i;
        }



        if( tmpStr.indexOf("servo",Qt::CaseInsensitive)>=0 )
        {
            devtype=E_EcatDev_Servo;


        }
        else  if( tmpStr.indexOf("rfid",Qt::CaseInsensitive)>=0 )
        {
            devtype=E_EcatDev_rfidSensor;
        }
        else  if( tmpStr.indexOf("maganetic",Qt::CaseInsensitive)>=0 )
        {
            devtype=E_EcatDev_maganeticSensor;
        }
        else  if( tmpStr.indexOf("battery",Qt::CaseInsensitive)>=0 )
        {
            devtype=E_EcatDev_batterySensor;
        }
        else  if( tmpStr.indexOf("iomodule",Qt::CaseInsensitive)>=0 )
        {
            devtype=E_EcatDev_IoModule;
        }
        else
        {
            addMsg(ENUM_MSG_ERROR,"EcatBusManager","EcatBusManager",12352,-1,0,0,0,0,tmpStr);
            devtype=E_EcatDev_New;
        }

        if( ok )
        {
            xmlFilelist_canopen.push_back(filepath+fileName);
            deviceTypeList_canopen.push_back( devtype);
            aliasList_canopen.push_back(alias);
            busIdList_canopen.push_back(busId);
            deviceIdList_canopen.push_back(deviceId);
            deviceChannel_canopen.push_back(tmpChannel);

            vendorId_canopen.push_back(vendorId);


        }
        else
        {
            qDebug()<<"不可用的总线配置,busDevs.xml文件格式有误!"<<i<<alias;
            return -3;
        }
    }

    closeXml();

    return 1;
}


int EcatBusManager::readVirtualDeviceXml( QString xmlfile )
{
    bool ok=openXml( xmlfile, QFile::ReadOnly | QFile::Text );
    if(!ok)
    {
        qDebug()<<"readBusXml error "<<ok;
        return -1;
    }
    aliasList_ethercat.clear();
    deviceTypeList_ethercat.clear();
    xmlFilelist_ethercat.clear();
    int ethercatIoDeviceVectorIndex=-1;
    int ethercatEncoderDeviceVectorIndex=-1;
    int ethercatMotorDeviceVectorIndex=-1;


    QDomNode cycleNode=findSubNode( m_root, "dcCycle", ok );
    if(!ok)
    {
       qDebug()<<"不可用的总线配置,busDevs.xml文件格式dcCycle有误!";
       return -2;
    }
    readXml( cycleNode, dcCycle );

    QDomNodeList devNodes = m_root.childNodes();
    int alias = 0;
    int  devtype ;
    QString tmpStr;
    QString filepath =D_TCRCONTROLLER_DATA_PATH;
    filepath+=GLOBAL_VIRTUAL_DEVICE_PATH;
    QString fileName ;
    for(int i=0;i<devNodes.size()-1;i++)
    {
        QDomNode devND =  devNodes.at(i);
        if( devND.nodeName() .indexOf("#comment")>=0){
            continue;
        }


        readXml( findSubNode( devND, "alias", ok ), alias );
        if(!ok)
        {
           return -100-i;
        }
        readXml( findSubNode( devND, "xmlFile", ok ), fileName );
        if(!ok)
        {
           return -100-i;
        }
        readXml( findSubNode( devND, "deviceType", ok ), tmpStr );
        if(!ok)
        {
           return -100-i;
        }



        if( tmpStr.indexOf("servo",Qt::CaseInsensitive)>=0 )
        {
            devtype=E_EcatDev_Servo;
            ethercatMotorDeviceVectorIndex+=1;
            ethercat_nodeAndVectorIndex.insert(i,ethercatMotorDeviceVectorIndex);

        }
        else  if( tmpStr.indexOf("coupler",Qt::CaseInsensitive)>=0 )
        {
            devtype=E_EcatDev_Coupler;
        }
        else  if( tmpStr.indexOf("iomodule",Qt::CaseInsensitive)>=0 )
        {
            devtype=E_EcatDev_IoModule;
            ethercatIoDeviceVectorIndex+=1;
            ethercat_nodeAndVectorIndex.insert(i,ethercatIoDeviceVectorIndex);
        }
        else  if( tmpStr.indexOf("encoderReceiver",Qt::CaseInsensitive)>=0 )
        {
            devtype=E_EcatDev_EncoderReceiver;
            ethercatEncoderDeviceVectorIndex+=1;
            ethercat_nodeAndVectorIndex.insert(i,ethercatEncoderDeviceVectorIndex);
        }
        else
        {
            addMsg(ENUM_MSG_ERROR,"EcatBusManager","EcatBusManager",12352,-1,0,0,0,0,tmpStr);
            devtype=E_EcatDev_New;
        }

        if( ok )
        {
            xmlFilelist_ethercat.push_back(filepath+fileName);
            deviceTypeList_ethercat.push_back( devtype);
            aliasList_ethercat.push_back(alias);
            deviceChannel_ethercat.push_back(1);
        }
        else
        {
            qDebug()<<"不可用的总线配置,busDevs.xml文件格式有误!"<<i<<alias;
            return -3;
        }
    }

    closeXml();

    return 1;
}


int EcatBusManager::readModBusRtuXml( QString xmlfile )
{

    bool ok=openXml( xmlfile, QFile::ReadOnly | QFile::Text );
    if(!ok)
    {
        qDebug()<<"readModBusRtuXml error "<<ok;
        return -1;
    }
    aliasList_modbus_rtu.clear();
    deviceTypeList_modbus_rtu.clear();
    xmlFilelist_modbus_rtu.clear();

    QDomNode cycleNode=findSubNode( m_root, "dcCycle", ok );
    if(!ok)
    {
       qDebug()<<"readModBusRtuXml ,不可用的总线配置,busDevs.xml文件格式dcCycle有误!";
       return -2;
    }
    readXml( cycleNode, dcCycle_modbus_rtu );

    QDomNodeList devNodes = m_root.childNodes();

    int alias = 0;
    int  devtype ;
    QString tmpStr;
    QString filepath=D_TCRCONTROLLER_DATA_PATH ;
    filepath+=GLOBAL_MODBUS_RTU_PATH;
    QString fileName ;
    for(int i=0;i<devNodes.size()-1;i++)
    {
        QDomNode devND =  devNodes.at(i);
        if( devND.nodeName() .indexOf("#comment")>=0){
            continue;
        }


        readXml( findSubNode( devND, "alias", ok ), alias );
        if(!ok)
        {
           return -100-i;
        }
        readXml( findSubNode( devND, "xmlFile", ok ), fileName );
        if(!ok)
        {
           return -100-i;
        }
        readXml( findSubNode( devND, "deviceType", ok ), tmpStr );
        if(!ok)
        {
           return -100-i;
        }



        if( tmpStr.indexOf("servo",Qt::CaseInsensitive)>=0 )
        {
            devtype=E_EcatDev_Servo;
        }else  if( tmpStr.indexOf("coupler",Qt::CaseInsensitive)>=0 ){
            devtype=E_EcatDev_Coupler;
        }else  if( tmpStr.indexOf("iomodule",Qt::CaseInsensitive)>=0 ){
            devtype=E_EcatDev_IoModule;
        }else{
            addMsg(ENUM_MSG_ERROR,"EcatBusManager","EcatBusManager",12352,-1,0,0,0,0,tmpStr);
            devtype=E_EcatDev_New;
        }

        if( ok )
        {
            xmlFilelist_modbus_rtu.push_back(filepath+fileName);
            deviceTypeList_modbus_rtu.push_back( devtype);
            aliasList_modbus_rtu.push_back(alias);
        }
        else
        {
            qDebug()<<"不可用的总线配置,readModBusRtuXml busDevs.xml文件格式有误!"<<i<<alias;
            return -3;
        }
    }

    closeXml();

    return 1;
}


int EcatBusManager::readModBusTcpXml( QString xmlfile )
{

    bool ok=openXml( xmlfile, QFile::ReadOnly | QFile::Text );
    if(!ok)
    {
        qDebug()<<"readModBusTcpXml error "<<ok;
        return -1;
    }
    aliasList_modbus_tcp.clear();
    deviceTypeList_modbus_tcp.clear();
    xmlFilelist_modbus_tcp.clear();

    QDomNode cycleNode=findSubNode( m_root, "dcCycle", ok );
    if(!ok)
    {
       qDebug()<<"readModBusTcpXml ,不可用的总线配置,busDevs.xml文件格式dcCycle有误!";
       return -2;
    }
    readXml( cycleNode, dcCycle_modbus_tcp );

    QDomNodeList devNodes = m_root.childNodes();

    int alias = 0;
    int  devtype ;
    QString tmpStr;
    QString filepath=D_TCRCONTROLLER_DATA_PATH ;
    filepath+=GLOBAL_MODBUS_TCP_PATH;
    QString fileName ;
    for(int i=0;i<devNodes.size()-1;i++)
    {
        QDomNode devND =  devNodes.at(i);
        if( devND.nodeName() .indexOf("#comment")>=0){
            continue;
        }


        readXml( findSubNode( devND, "alias", ok ), alias );
        if(!ok)
        {
           return -100-i;
        }
        readXml( findSubNode( devND, "xmlFile", ok ), fileName );
        if(!ok)
        {
           return -100-i;
        }
        readXml( findSubNode( devND, "deviceType", ok ), tmpStr );
        if(!ok)
        {
           return -100-i;
        }



        if( tmpStr.indexOf("servo",Qt::CaseInsensitive)>=0 )
        {
            devtype=E_EcatDev_Servo;
        }else  if( tmpStr.indexOf("coupler",Qt::CaseInsensitive)>=0 ){
            devtype=E_EcatDev_Coupler;
        }else  if( tmpStr.indexOf("iomodule",Qt::CaseInsensitive)>=0 ){
            devtype=E_EcatDev_IoModule;
        }else{
            addMsg(ENUM_MSG_ERROR,"EcatBusManager","EcatBusManager",12352,-1,0,0,0,0,tmpStr);
            devtype=E_EcatDev_New;
        }

        if( ok )
        {
            xmlFilelist_modbus_tcp.push_back(filepath+fileName);
            deviceTypeList_modbus_tcp.push_back( devtype);
            aliasList_modbus_tcp.push_back(alias);
        }
        else
        {
            qDebug()<<"不可用的总线配置,readModBusTcpXml busDevs.xml文件格式有误!"<<i<<alias;
            return -3;
        }
    }

    closeXml();
    return 1;
}


int EcatBusManager::readMotorConfigXml(QString filepath,QVector<SdoConfig> &sdoConfigListOut)
{

    sdoConfigListOut.clear();
    DomParser domparser;
    bool ok=domparser.openXml(filepath, QFile::ReadOnly|QFile::Text );
    if(!ok)
    {
        qDebug()<<"error,,,EcatBusManager::readMotorConfigXml :"<<ok<< filepath;
        return -1;
    }

//    int tmpId;
//    domparser.readXml( domparser.findSubNode(domparser.getRootDomElement(), "vendorId", ok), tmpId  );
//    if( !ok)
//    {

//        qDebug()<<"McControllerInterpreter::readRobotInfo====读文件失败: robotType";
//        return -1;
//    }
//    vendorId=(E_VENDOR_ID)tmpId;

    QDomNode domNode = domparser.findSubNode(domparser.getRootDomElement(), "sdoSetting", ok );
    if( !ok )
    {
            qDebug() << " 读取结点失败：" << filepath;
            return -1;
    }

    QDomNodeList devNodes = domNode.childNodes();
    int ncount=devNodes.size();

    SdoConfig tmpSdoConfig;
    for(int i=0;i<ncount;i++)
    {
        QDomNode devND =  devNodes.at(i);
        if( devND.nodeName().indexOf("#comment")>=0){
            continue;
        }

        bool isSucess;
        tmpSdoConfig.index=  devND.toElement().attribute("index" ).toUInt(&isSucess,16);
        tmpSdoConfig.subindex=  devND.toElement().attribute("subindex" ).toUInt(&isSucess,16);
        tmpSdoConfig.byteLength=  devND.toElement().attribute("byteLength" ).toUInt(&isSucess,16);
        tmpSdoConfig.value=  devND.toElement().attribute("value" ).toUInt(&isSucess,16);
        sdoConfigListOut.append(tmpSdoConfig);

    }

    domparser.closeXml();


    return 1;

}



int EcatBusManager::addVirtualDevices( )
{
//    EcatBusDevInfo devInfo[]={
//        { "Coupler",  0, 81, 0x1dd, 0x0, 0,0, E_EcatDev_Coupler, "对象字典配置文件xml"},
//        { "io",   1, 82, 0x1dd, 0x0, 0,0, E_EcatDev_IoModule, "io对象字典配置文件xml"},
//        { "io",   2, 83, 0x1dd, 0x0, 0,0, E_EcatDev_IoModule, "io对象字典配置文件xml"},
//    };
//    for( int i=0; i<3; i++ )
//    {
//        addDevice( devInfo[i] );
//    }

    //仿真时，仅支持配置motor, io不支持配置
    EcatBusDevInfo devInfo_servo={ "servo",  0, 0, 0x1dd, 0x0, 0,0, E_EcatDev_Servo, "servo.xml"};
    for( int i=0;i<aliasList_ethercat.size();i++ )
    {
        devInfo_servo.node= i;
        devInfo_servo.alias=aliasList_ethercat[i];
        addDevice( devInfo_servo );
    }
    return 1;
}

#if 1
void EcatBusManager::addDevice(  const EcatBusDevInfo &devInfo )
{
    int node = devInfo.node;
    if(node>D_MAX_SLAVES){
        printf("Error:设备站号超过最大限制 ：node=%d ,alias=%d \n",node, ec_devInfo[node].devInfo.alias);
    }
    if( ec_devInfo[node].devInfo.alias != 0 )
    {
        printf("Warn:设备信息站号重复,覆盖：node=%d ,alias=%d \n",node, ec_devInfo[node].devInfo.alias);
    }else{
        ethercatDeviceSize++;
    }

    ec_devInfo[node].devInfo = devInfo;
    nodeToRobotIdAndAxis[node].devType=devInfo.devType;

    _isMapOk=false;
    _isLinkOk=false;
    printf("Msg:添加设备：node=%d ,alias=%d \n",node, ec_devInfo[node].devInfo.alias);

}
#else
void EcatBusManager::addDevice(
        unsigned int node,
        unsigned int alias,
        unsigned int vendorId,
        unsigned int productCode,
        unsigned int revisionNumber )
{
    if(node>D_MAX_SLAVES){
        printf("Error:设备站号超过最大限制 ：node=%d ,alias=%d \n",node, ec_devInfo[node].devInfo.alias);
    }
    if( ec_devInfo[node].devInfo.alias != 0 )
    {
        printf("Warn:设备信息站号重复,覆盖：node=%d ,alias=%d \n",node, ec_devInfo[node].devInfo.alias);
    }else{
        _devcunt++;
    }

    int devtype = getMapVal( vendorId, productCode );
    if(devtype==E_EcatDev_Servo){
        ec_devInfo[node].devInfo.devName = "servo";
        ec_devInfo[node].devInfo.devType= 0;
        ec_devInfo[node].devInfo.mappingObjectFile= "servo.xml";
    }else if(devtype==E_EcatDev_IoModule){
        ec_devInfo[node].devInfo.devName = "io";
        ec_devInfo[node].devInfo.devType= 0;
        ec_devInfo[node].devInfo.mappingObjectFile= "servo.xml";
    }else {
        ec_devInfo[node].devInfo.devName = "coupler";
        ec_devInfo[node].devInfo.devType= 0;
        ec_devInfo[node].devInfo.mappingObjectFile= "servo.xml";
    }
//    ec_devInfo[node].devInfo.devName = "servo";
//    ec_devInfo[node].devInfo.devType= 0;
//    ec_devInfo[node].devInfo.mappingObjectFile= "servo.xml";



//    struct S_EcatDevTypeMap{
//        u64 ven;
//        u64 pro;
//    };

    ec_devInfo[node].devInfo.node=node;
    ec_devInfo[node].devInfo.alias=alias;
    ec_devInfo[node].devInfo.vendorId=vendorId;
    ec_devInfo[node].devInfo.productCode=productCode;
    ec_devInfo[node].devInfo.revisionNumber=revisionNumber;
    ec_devInfo[node].devInfo.state=0;


    nodeToRobotIdAndAxis[node].devType=devInfo.devType;

    _isMapOk=false;
    _isLinkOk=false;
    printf("Msg:添加设备：node=%d ,alias=%d \n",node, ec_devInfo[node].devInfo.alias);

}
#endif


int  EcatBusManager::addModbusRtuDeviceTORobotBase(int& aliasOut)
{
    RobotBase* rob;
    foreach ( rob, robCtl->robotsMap )
    {
        if( rob )
        {
            rob->modbusRtuAxis.clear();
            rob->modbusRtuIoModule.clear();

            int node=0;

            //RobotBase添加　motor , io

            for( int i=0;i<rob->robotInfo.motorAlias.size();i++ )
            {
                if(E_DEVICE_PROTOCOL_MODBUS_RTU==rob->robotInfo.motorDeviceProtocol[i])
                {
                    if( 1==getIndexByAlias_MotorVector(modbus_rtu_motors,rob->robotInfo.motorAlias[i], node) )
                    {
                        rob->modbusRtuAxis.append( modbus_rtu_motors[node] );
                        rob->logicAxis[i]=modbus_rtu_motors[node]->logicMotors[0];
                        rob->logicalAxisArryCopy[i]=modbus_rtu_motors[node]->logicMotors[0];
                        rob->size_logicalAxisArryCopy+=1;


                    }
                    else
                    {
                        aliasOut=rob->robotInfo.motorAlias[i];
                        return -1-i;
                    }
                }
            }
            for( int i=0;i<rob->robotInfo.modbus_rtu_ioAlias.size();i++ )
            {

                    if( 1==getIndexByAlias_IoModuleVector(modbus_rtu_ioModule,rob->robotInfo.modbus_rtu_ioAlias[i], node))
                    {
                        rob->modbusRtuIoModule.append( modbus_rtu_ioModule[node] );

                    }
                    else
                    {
                        aliasOut=rob->robotInfo.modbus_rtu_ioAlias[i];
                        return -1000-i;
                    }

            }

        }

    }


    return 1;
}

int EcatBusManager::addCanopenDeviceTORobotBase(int &aliasOut)
{

    RobotBase* rob;
    foreach ( rob, robCtl->robotsMap )
    {
        if( rob )
        {
            rob->canopenAxis.clear();
            rob->canopenIoModule.clear();

            int node=0;

            //RobotBase添加　motor , io
            for( int i=0;i<rob->robotInfo.motorAlias.size();i++ )
            {
                if(E_DEVICE_PROTOCOL_CANOPEN==rob->robotInfo.motorDeviceProtocol[i])
                {
                    if( 1==getIndexByAlias_MotorVector(canopen_motors,rob->robotInfo.motorAlias[i], node) )
                    {
                        rob->canopenAxis.append( canopen_motors[node] );

                        rob->logicAxis[rob->size_logicalAxisArryCopy]=canopen_motors[node]->logicMotors[rob->robotInfo.motorChannel[i]];
                        rob->logicalAxisArryCopy[rob->size_logicalAxisArryCopy]=rob->logicAxis[rob->size_logicalAxisArryCopy];
                        rob->size_logicalAxisArryCopy+=1;



                    }
                    else
                    {
                        aliasOut=rob->robotInfo.motorAlias[i];
                        return -1-i;
                    }
                }
            }

            for( int i=0;i<rob->robotInfo.canopen_ioAlias.size();i++ )
            {

                    if( 1==getIndexByAlias_IoModuleVector(canopen_ioModule,rob->robotInfo.canopen_ioAlias[i], node))
                    {
                        rob->canopenIoModule.append( canopen_ioModule[node] );

                    }
                    else
                    {
                        aliasOut=rob->robotInfo.canopen_ioAlias[i];
                        return -1000-i;
                    }

            }

        }

    }


    return 1;
}

int  EcatBusManager::addModbusTcpDeviceTORobotBase(int &aliasOut)
{
    RobotBase* rob;
    foreach ( rob, robCtl->robotsMap )
    {
        if( rob )
        {
            rob->modbusTcpAxis.clear();
            rob->modbusTcpIoModule.clear();

            int node=0;

            //RobotBase添加　motor , io

            for( int i=0;i<rob->robotInfo.motorAlias.size();i++ )
            {
                if(E_DEVICE_PROTOCOL_MODBUS_TCP==rob->robotInfo.motorDeviceProtocol[i])
                {
                    if( 1==getIndexByAlias_MotorVector(modbus_tcp_motors,rob->robotInfo.motorAlias[i], node) )
                    {
                        rob->modbusTcpAxis.append( modbus_tcp_motors[node] );
                        rob->logicAxis[i]=modbus_tcp_motors[node]->logicMotors[0];
                        rob->logicalAxisArryCopy[i]=modbus_tcp_motors[node]->logicMotors[0];
                        rob->size_logicalAxisArryCopy+=1;

                    }
                    else
                    {
                        aliasOut=rob->robotInfo.motorAlias[i];
                        return -1-i;
                    }
                }
            }
            for( int i=0;i<rob->robotInfo.modbus_tcp_ioAlias.size();i++ )
            {

                    if( 1==getIndexByAlias_IoModuleVector(modbus_tcp_ioModule,rob->robotInfo.modbus_tcp_ioAlias[i], node))
                    {
                        rob->modbusTcpIoModule.append( modbus_tcp_ioModule[node] );

                    }
                    else
                    {
                        aliasOut=rob->robotInfo.modbus_tcp_ioAlias[i];
                        return -1000-i;
                    }

            }

        }

    }


    return 1;
}

int EcatBusManager::checkRobotLogicalAxisMapEthercatAxis(int& nodeReturn,int& aliasReturn)
{
    if(_isMapOk)
    {
        return 1;
    }
    if(!robCtl)
    {
        return -1;
    }

    bool checkMap=true;
    RobotBase* rob;
    foreach ( rob, robCtl->robotsMap )
    {
        if( rob )
        {
            rob->ethercatAxis.clear();
            rob->ethercatIoModule.clear();
            int node=0;

            //RobotBase添加　motor , io
            int tmpVectorIndex=-1;
            for( int tmpMotorAliasIndex=0;tmpMotorAliasIndex<rob->robotInfo.motorAlias.size();tmpMotorAliasIndex++ )
            {
                if(E_DEVICE_PROTOCOL_ETHERCAT==rob->robotInfo.motorDeviceProtocol[tmpMotorAliasIndex])
                {
                    if( getEthercatNodeIdByAlias(rob->robotInfo.motorAlias.at(tmpMotorAliasIndex), node) )
                    {

                        //没有检查设备类型是否对应，io motor
                        if(aliasList_ethercat[node]!=rob->robotInfo.motorAlias.at(tmpMotorAliasIndex))
                        {
                            nodeReturn=node;
                            aliasReturn=rob->robotInfo.motorAlias.at(tmpMotorAliasIndex);
                            return -100;
                        }
                        getDeviceArryIndexByNodeId_ethercatDevice(node,tmpVectorIndex);
                        rob->ethercatAxis.append( ec_motors[tmpVectorIndex] );
                        rob->add_ethercat_node_info(&ec_devInfo[node]);
                        nodeToRobotIdAndAxis[node].axis=tmpMotorAliasIndex;
                        nodeToRobotIdAndAxis[node].robotId=rob->robotInfo.robotId;
                        nodeToRobotIdAndAxis[node].devType=ec_devInfo[node].devInfo.devType;
                        int tmpSize1=rob->logicAxis.size();
                        int tmpSize2=ec_motors.size();
                        if(rob->size_logicalAxisArryCopy<0 || rob->size_logicalAxisArryCopy>=rob->logicAxis.size())
                        {
                            qDebug()<<"rob->size_logicalAxisArryCopy<0 || rob->size_logicalAxisArryCopy>=rob->logicAxis.size()"
                                   <<rob->size_logicalAxisArryCopy;
                            return -191;
                        }
                        int tmpMotorIndex;
                        if(1!=getDeviceArryIndexByNodeId_ethercatDevice(node,tmpMotorIndex))
                        {
                            qDebug()<<"1!=getDeviceArryIndexByNodeId_ethercatDevice(node,tmpMotorIndex)"<<node;
                            return -192;
                        }
                        if(tmpMotorIndex<0 || tmpMotorIndex>=ec_motors.size())
                        {
                            qDebug()<<"tmpMotorIndex<0 || tmpMotorIndex>=ec_motors.size()"<<tmpMotorIndex;
                            return -192;
                        }
                        if(tmpMotorAliasIndex<0 || tmpMotorAliasIndex>=rob->robotInfo.motorChannel.size())
                        {
                            qDebug()<<"tmpMotorAliasIndex<0 || tmpMotorAliasIndex>=rob->robotInfo.motorChannel.size()"
                                   <<tmpMotorAliasIndex;
                            return -193;
                        }
                        if(rob->robotInfo.motorChannel[tmpMotorAliasIndex]<0
                                || rob->robotInfo.motorChannel[tmpMotorAliasIndex]>=D_MAX_MOTOR_CHANNEL)
                        {
                            qDebug()<<"rob->robotInfo.motorChannel[tmpMotorAliasIndex]<0 "
                                "|| rob->robotInfo.motorChannel[tmpMotorAliasIndex]>=D_MAX_MOTOR_CHANNEL"
                                   <<rob->robotInfo.motorChannel[tmpMotorAliasIndex];
                            return -194;
                        }
                        rob->logicAxis[rob->size_logicalAxisArryCopy]=ec_motors[tmpMotorIndex]->
                                         logicMotors[rob->robotInfo.motorChannel[tmpMotorAliasIndex]];
                        rob->logicalAxisArryCopy[rob->size_logicalAxisArryCopy]=rob->logicAxis[rob->size_logicalAxisArryCopy];
                        rob->size_logicalAxisArryCopy+=1;

                    }
                    else
                    {
                        aliasReturn=rob->robotInfo.motorAlias.at(tmpMotorAliasIndex);
                        checkMap=false;
                        return -2;
                    }
                }
            }
            for( int i=0;i<rob->robotInfo.ethercat_ioAlias.size();i++ )
            {

                if( getEthercatNodeIdByAlias(rob->robotInfo.ethercat_ioAlias.at(i), node) )
                {
                    if(aliasList_ethercat[node]!=rob->robotInfo.ethercat_ioAlias.at(i))
                    {
                        nodeReturn=node;
                        aliasReturn=rob->robotInfo.ethercat_ioAlias.at(i);
                        return -101;
                    }
                    getDeviceArryIndexByNodeId_ethercatDevice(node,tmpVectorIndex);
                    ec_ioModule[tmpVectorIndex]->nodeId=ec_devInfo[node].devInfo.alias;
                    rob->ethercatIoModule.append( ec_ioModule[tmpVectorIndex] );
                    rob->add_ethercat_node_info(&ec_devInfo[node]);
                    nodeToRobotIdAndAxis[node].axis=i;
                    nodeToRobotIdAndAxis[node].robotId=rob->robotInfo.robotId;
                    nodeToRobotIdAndAxis[node].devType=ec_devInfo[node].devInfo.devType;
                }
                else
                {
                    aliasReturn=rob->robotInfo.ethercat_ioAlias.at(i);
                    checkMap=false;
                    return -3;
                }
            }
            if( checkMap )
            {
                rob->isLinkOk=true;
            }else
            {
                //TODD .. error
            }
        }

    }

    if(checkMap)
    {
        _isMapOk=true;
        _isLinkOk=true;
    }
    else
    {
        return -2;
    }

    return 1;
}

int EcatBusManager::getDeviceArryIndexByAlias_ethercatDevice(int alias, int &vectorIndexOut)
{
    int tmpNode;
    if( getEthercatNodeIdByAlias(alias, tmpNode))
    {
        if(1==getDeviceArryIndexByNodeId_ethercatDevice(tmpNode,vectorIndexOut))
        {
            return 1;
        }
        return 0;
    }
    else
    {
        return 0;
    }
    return 0;
}

int EcatBusManager::getDeviceArryIndexByNodeId_ethercatDevice(int nodeId, int &vectorIndexOut)
{
    if(false==ethercat_nodeAndVectorIndex.contains(nodeId))
    {
        return -1;
    }
    vectorIndexOut=ethercat_nodeAndVectorIndex.value(nodeId);
    return 1;
}

int EcatBusManager::getIndexByAlias_MotorVector(QVector<EcatMotor*> motorVector,int alias,int& indexOut)
{
    for(int i=0;i<motorVector.size();i++)
    {
        if(alias==motorVector[i]->getAlias())
        {
            indexOut=i;
            return 1;
        }
    }
    return -1;
}

int EcatBusManager::getIndexByAlias_IoModuleVector(QVector<EcatIo*> ioModuleVector,int alias,int& indexOut)
{

    for(int i=0;i<ioModuleVector.size();i++)
    {
        if(alias==ioModuleVector[i]->getAlias())
        {
            indexOut=i;
            return 1;
        }
    }
    return -1;
}

bool EcatBusManager::getEthercatNodeIdByAlias(int alias, int&node)
{
    for(int i =0;i< ethercatDeviceSize; i++)
    {
        if( ec_devInfo[i].devInfo.alias == alias )
        {
            node=ec_devInfo[i].devInfo.node;
            return true;
        }
    }
    return false;
}



#if 0
void EcatBusManager::initAliasMapNode()
{
    for(int i =0;i< _devcunt; i++){
        aliasMapDevInfo.insert( ec_devInfo[i].devInfo.alias, &ec_devInfo[i] );
    }
}
#endif



void EcatBusManager::addMsg(int messageLevel, string componentName, string messageType,
                           int messageCode, int robotId,
                           int parameter1, int parameter2, int parameter3, int parameter4,QString message)
{
    Message tmpMsg;
    tmpMsg.MessageLevel = messageLevel;
    tmpMsg.componentClassName = componentName;
    tmpMsg.messageType = messageType;
    tmpMsg.robotId = robotId;
    tmpMsg.messageCode = messageCode;
   // tmpMsg.time = t1.tv_sec;


    QString infomationStr;

    switch(messageCode)
    {
    case 12354:
    {
        infomationStr =  QObject::tr("机器人:")  + QString::number(robotId);
        infomationStr = infomationStr +
                QObject::tr(":io模块sdo配置文件读取失败：%2！").arg(message)
                .arg(message);
        break;


    }
    case 12353:
    {
        infomationStr =  QObject::tr("机器人:")  + QString::number(robotId);
        infomationStr = infomationStr +
                QObject::tr(":ethercat 电机sdo配置文件读取失败：%2！").arg(message)
                .arg(message);
        break;


    }
    case 12352:
    {
        infomationStr =  QObject::tr("机器人:")  + QString::number(robotId);
        infomationStr = infomationStr +
                QObject::tr(":未知的设备类型%2！").arg(parameter1)
                .arg(message);
        break;


    }
    case 12351:
    {
        infomationStr =  QObject::tr("机器人:")  + QString::number(robotId);
        infomationStr = infomationStr +
                QObject::tr(":编码器接收模块配置文件初始化失败！").arg(parameter1)
                .arg(message);
        break;


    }

    default:
    {

    }
    }


    tmpMsg.MessageInformation = infomationStr.toStdString();

    // qDebug()<<"53444444444444444444444"<< tmpMsg.messageCode;
    MessageLog::getInstance()->addMessage(tmpMsg);
}
